import { queryProcedure, queryProcedureProxy,queryProcedureCompleted, queryProcedureTodo } from '@/services/api';

export default {
  namespace: 'procedure',
  state: {
    procedureList: [],
    todoList: [],
    completedList: [],
    proxyList: [],
  },

  effects: {
    *fetch(_, { call, put }) {
      const response = yield call(queryProcedure);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchTodo(_, { call, put }) {
      const response = yield call(queryProcedureTodo);
      yield put({
        type: 'saveTodo',
        payload: response,
      });
    },
    *fetchCompleted(_, { call, put }) {
      const response = yield call(queryProcedureCompleted);
      yield put({
        type: 'saveCompleted',
        payload: response,
      });
    },
    *fetchProxy(_, { call, put }) {
      const response = yield call(queryProcedureProxy);
      yield put({
        type: 'saveProxy',
        payload: response,
      });
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        procedureList: payload,
      };
    },
    saveTodo(state, { payload }) {
      return {
        ...state,
        todoList: payload,
      };
    },
    saveCompleted(state, { payload }) {
      return {
        ...state,
        completedList: payload,
      };
    },

    saveProxy(state, { payload }) {
      return {
        ...state,
        proxyList: payload,
      };
    },

    clear() {
      return {
        procedureList: [],
        todoList: [],
        completedList: [],
        proxyList: [],
      };
    },
  },
};
